<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="css/loginRegister.css"/>
    <title>iBOOK-登录</title>
</head>
<body id="loginPage">
<form>
    <p>
        <input type="text" name="user_name" id="user_name" placeholder="账号"/>
    </p>
    <p>
        <input type="password" name="pswd" id="pswd" placeholder="密码" maxlength="6"/>
    </p>
    <p>
        <input type="button" value="登录" id="login"/>
    </p>
    <p>
        <a href="#" id="left">手机短信登录</a>
        <span id="right">
				<a href="register.html">注册</a> |
				<a href="#">忘记密码</a> |
				<a href="index.html">首页</a>
			</span>
    </p>
</form>
</body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    window.onload = function(){
        document.getElementById('login').onclick = function() {
            let name = document.querySelector("#user_name").value;
            let pswd = document.querySelector("#pswd").value;

            // 简单验证
            if (!name || !pswd) {
                alert("用户名和密码不能为空");
                return;
            }
            axios({
                method: 'post', // 务必改用POST，避免密码暴露（之前的GET问题也要修！）
                url: '/dem1/UserController/login',
                data: {
                    user_name: name,
                    pswd: pswd
                }
            }).then(response => {
                let obj = response.data;
                if (obj == null || obj == '') {
                    alert("用户名或密码错误！");
                } else {
                    // 登录成功，跳转到主页面
                    location = "/dem1/index.html";
                    //console.log(obj);
                }
            })
        }
    }
</script>


</html>